
#include "palette.h"
#include "image.h"
#include "maths.h"

extern const int g_palette[256];

void PaletteInit(palette_t& p)
{
	for(int i=0;i<256;i++) {		
		p.colors[i]=(pixel_t)g_palette[i];
	}
	PaletteBuildIntensities(p);
}

void PaletteInit(palette_t& p,const TCHAR *filename)
{

	bitmapfile_t b;
	if (!BitmapLoad(b,filename)) {
		return;
	}

	if (b.infoheader.biBitCount==24) {
		PaletteInit(p);
		return;
	}

	for(int index=0;index<256;index++) {
		u8 red=b.palette[index].peRed		>> 3,
		   green=b.palette[index].peGreen	>> 2,
		   blue=b.palette[index].peBlue		>> 3;
		p.colors[index]=RGB_SET(red,green,blue);
	}
	
	BitmapFree(b);
	PaletteBuildIntensities(p);
}

void PaletteBuildIntensities(palette_t& p)
{
	int ri,gi,bi;
	float rw,gw,bw;
	float dl,dr,db,dg;

	for(int col_index=0; col_index<256; col_index++) {
		ri=RGB_RED(p.colors[col_index]);
		gi=RGB_GREEN(p.colors[col_index]);
		bi=RGB_BLUE(p.colors[col_index]);

		//ri+=8;gi+=16;bi+=8;	// white
		//ri+=4;gi-=4;bi-=2;	// red
		//ri-=2;gi+=6;bi-=1;	// green
		//ri-=2;gi-=1;bi+=8;	// blue

		if (ri>31) ri=31;
		if (gi>63) gi=63;
		if (bi>31) bi=31;
		if (ri<0) ri=0;
		if (gi<0) gi=0;
		if (bi<0) bi=0;

		// compute steps
		dl = (float)INTENSITY_ACCURACY;
		dr = ri/dl;
		dg = gi/dl;
		db = bi/dl;

		// init working color
		rw = 0.0f;
		gw = 0.0f;
		bw = 0.0f;

		for(int intensity_index=0;intensity_index<dl;intensity_index++) {
			p.intensity[col_index][intensity_index]=RGB_SET((int)rw,(int)gw,(int)bw);
			if ((rw+=dr)>31.0f) rw=31.0f;
			if ((gw+=dg)>63.0f) gw=63.0f;
			if ((bw+=db)>31.0f) bw=31.0f;
		}
	}
}

const int g_palette[256]=
{
0,16777215,6118374,3617226,4867818,4012212,4933817,10065864,2893935,3815048,
3815521,10064117,3549356,1841208,3152041,2823567,4796363,2692974,4004034,4729015,
14473197,2627672,1968200,2627656,1640504,3811417,2558041,1640488,3741784,10590377,
3672154,3876936,3741760,9932952,4130885,5785689,3680312,4864074,16312568,2693160,
6903912,9009288,1706008,8022136,6834265,4728888,6563901,5248809,7948376,6555936,
11675722,7870511,11417679,13718375,15627141,16222349,7481385,8079176,1771528,4859944,
2693144,5781560,3680296,7952472,4732984,8087656,6838360,4920339,6834247,10047305,
10446687,5785670,8992810,6830256,7878699,10045496,11098185,11442838,16312548,8734776,
11960946,9210248,7287063,9979944,11098168,9402736,8083528,5122069,9984056,8734760,
5781542,10045464,13137991,11629129,10049576,11102262,8538136,13484213,15318680,14407635,11579310,
8869927,8281400,12558203,6838342,11368503,9922608,4732965,14660736,13025610,7628886,
11766088,13082972,9073990,15654057,5983521,2959383,1710088,13288848,12171388,3684391,
5789766,7895142,16316647,4737095,6977546,12635520,8625522,3623213,4873281,10135955,
8960375,7048031,5405255,2635815,530440,1517591,15202535,15132902,14018775,5994593,
5072980,8776621,3692614,1587239,538663,2644040,6519416,2508870,2644568,538680,
534568,530456,1587256,1583144,3692632,2635832,3688520,4741208,15137016,11662839,
547681,5150878,5613751,4903918,8970224,7375246,1403003,3500148,6470612,4421766,
7645366,5269615,1460312,4032931,7391720,5605529,9218740,5351628,7051433,5609653,
534584,10009291,7895675,1724783,3364973,4225177,7514572,6125704,2639965,5675208,
12178922,536398,2373704,7779044,4417672,530472,2907535,1583160,6854856,5798040,
3047381,6198745,6129850,4295657,4352155,3688541,5535914,14543096,12699600,2177118,
8034503,9283795,530488,7903448,7702701,10861542,2447563,9152744,7903464,7043224,
5265516,6121608,2047151,1452147,5990554,7833544,8952024,8952040,7174314,5134476,
9147062,2304072,7898333,1054544,1976974,5594298,2500696,4540550,526397,1250443,
1184877,526376,526360,1579048,2631736,3684424,4737116,6842488,16316664,6842472,
5789784,3684408,2631720,1579032,526344
};
